package com.ubs.training.server.aop;

import com.ubs.training.prod.server.service.LoggingService;
import com.ubs.training.server.dao.model.UBSContact;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/24/12
 * Time: 8:53 AM
 * To change this template use File | Settings | File Templates.
 */
@Aspect
@Component
public class LogginAspectAdvice extends AbstractLoggingAdvice {

    @Before("execution(* com.ubs.training.server.service.UBSContactService.*(..))")
    public void logAfterUBS(JoinPoint join){
        String methodName = join.getSignature().getName();
        Object[] incomArgs = join.getArgs();
        String args = Arrays.toString(incomArgs);
        if(incomArgs.length == 1){
            if(incomArgs[0] instanceof UBSContact){
                UBSContact ubsCont =(UBSContact)incomArgs[0];
                if(ubsCont.getId() == 0){
                    svc.logEvent("User call method - " + methodName + " with parameter to add new Object" + " in UBSContactService");
                }else{
                    svc.logEvent("User call method - " + methodName + " with parameter to update the Object with id - " + ubsCont.getId() + " in UBSContactService");
                }
            }else{
                svc.logEvent("User call method - " + methodName + " for Object with id -" + incomArgs[0] + " in UBSContactService");
            }
        }
        if(incomArgs.length == 0){
            svc.logEvent("User call method - " + methodName + " in UBSContactService");
        }
    }
                                       /*
    @Around("execution(* com.ubs.training.server.service.UBSContactService.*(..))")
    public void logArounUBS(ProceedingJoinPoint join){
        String methodName = join.getSignature().getName();
        Object[] incomArgs = join.getArgs();
        String args = Arrays.toString(incomArgs);
        if(incomArgs.length == 1){
            if(incomArgs[0] instanceof UBSContact){
                UBSContact ubsCont =(UBSContact)incomArgs[0];
                if(ubsCont.getId() == 0){
                    svc.logEvent("User call method - " + methodName + " with parameter to add new Object" + " in UBSContactService");
                }else{
                    svc.logEvent("User call method - " + methodName + " with parameter to update the Object with id - " + ubsCont.getId() + " in UBSContactService");
                }
            }else{
                svc.logEvent("User call method - " + methodName + " for Object with id -" + incomArgs[0] + " in UBSContactService");
            }
        }
        if(incomArgs.length == 0){
            svc.logEvent("User call method - " + methodName + " in UBSContactService");
        }
        try {
            join.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
*/
    @After("execution(* com.ubs.training.server.service.UBSContactService.*(..))")
    public void logAfterEmployee(JoinPoint join){
        String methodName = join.getSignature().getName();
        Object[] incomArgs = join.getArgs();
        String args = Arrays.toString(incomArgs);
        if(incomArgs.length == 1){
            svc.logEvent("User call method - " + methodName + " for Object with id -" + incomArgs[0] + " in EmplyeeService");
        }
        if(incomArgs.length == 0){
            svc.logEvent("User call method - " + methodName + " in EmployeeService");
        }
    }

    @After("execution(* com.ubs.training.server.service.ContactTypeService.*(..))")
    public void logAfterContactType(JoinPoint join){
        String methodName = join.getSignature().getName();
        Object[] incomArgs = join.getArgs();
        String args = Arrays.toString(incomArgs);
        if(incomArgs.length == 0){
            svc.logEvent("User call method - " + methodName + " in ContactTypeService");
        }
    }

}
